package com.ktjiaoyu;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisTest {
    @Test
    public void testString(){
        //1获得链接
        Jedis jedis=new Jedis("127.0.0.1",6379);
        //2.操作
        //存储一个键值对
        jedis.set("username","张三");
        //获取键对应的值
        System.out.println(jedis.get("username"));
        System.out.println("-----------------");
        //存储key的同时设置过期时间
        jedis.setex("checkCode",30,"1234");
        //过期时间后键值对会自动删除
        System.out.println(jedis.get("checkCode"));
        System.out.println("---------------------");
        //一次设置多个键值对
        jedis.mset("name","李四","age","18");
        System.out.println(jedis.get("name"));
        System.out.println(jedis.get("age"));
        System.out.println("------------------");
        //年龄key的值在原有的基础上加1
        jedis.incr("age");
        System.out.println(jedis.get("age"));
        //关闭链接
        jedis.close();

    }

    @Test
    public void testHash(){
        //1.获取链接
        Jedis jedis=new Jedis("127.0.0.1",6379);
        //操作
        //存储三个键值对
        jedis.hset("myhash","name","李四");
        jedis.hset("myhash","age","34");
        jedis.hset("myhase","gender","man");
        //获取name这个键值对对应的值
        String string = jedis.hget("myhash", "name");
        System.out.println(string);
        //获取myhash里面所有的键值对
        Map<String,String> map=jedis.hgetAll("myhash");
        for (String s : map.keySet()) {
            System.out.println(s+":"+map.get(s));
        }
        jedis.close();
    }
    @Test
    public void testList() {
        //获得链接
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //2.操作
        jedis.lpush("mylist", "a", "b", "c");
        jedis.rpush("mylist", "a", "b", "c");
        //获取所有数据
        List<String> list = jedis.lrange("mylist", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
        //删除左边的数据
        String lpop = jedis.lpop("mylist");
        System.out.println(lpop);

        //删除右边的数据
        String rpop = jedis.rpop("mylist");
        System.out.println(rpop);

        //获取所有的数据
        list = jedis.lrange("mylist", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
        jedis.close();
    }
    @Test
    public void testSet(){
        Jedis jedis=new Jedis();
        jedis.sadd("myset","a","b","c");
        Set<String> list = jedis.smembers("myset");
        for (String s : list) {
            System.out.println(s);
        }
        jedis.close();
    }

    @Test
    public void testSortedset() {
            //1. 获得连接
        Jedis jedis=new Jedis("127.0.0.1", 6379);
            //2. 操作
        jedis.zadd("mysortedset", 80,"张三");
        jedis.zadd("mysortedset", 70,"李四");
        jedis.zadd("mysortedset", 90,"王五");
            //获取所有数据
        Set<String> set = jedis.zrange("mysort", 0, -1);
        for (String string : set) {
            System.out.println(string);
        }
            //3. 关闭连接
        jedis.close();
    }

    @Test
   public void testPool(){
        Jedis jedis=new Jedis();
        jedis.set("username","张三");
       System.out.println(jedis.get("username"));
       JedisPoolUtils.closeJedis(jedis);
   }
}
